package com.ruoyi.datascreen.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.datascreen.domain.bo.DsCustomDataBo;
import com.ruoyi.datascreen.domain.vo.DsCustomDataVo;
import com.ruoyi.datascreen.domain.DsCustomData;
import com.ruoyi.datascreen.mapper.DsCustomDataMapper;
import com.ruoyi.datascreen.service.IDsCustomDataService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 自定义数据集Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-28
 */
@RequiredArgsConstructor
@Service
public class DsCustomDataServiceImpl implements IDsCustomDataService {

    private final DsCustomDataMapper baseMapper;

    /**
     * 查询自定义数据集
     */
    @Override
    public DsCustomDataVo queryById(Long customId){
        DsCustomDataVo customDataVo = baseMapper.selectVoById(customId);
        customDataVo.setTypeDataList(JSONArray.parse(customDataVo.getTypeList()));
        customDataVo.setDataDataList(JSONArray.parse(customDataVo.getDataList()));
        return customDataVo;
    }

    /**
     * 查询自定义数据集列表
     */
    @Override
    public TableDataInfo<DsCustomDataVo> queryPageList(DsCustomDataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DsCustomData> lqw = buildQueryWrapper(bo);
        Page<DsCustomDataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询自定义数据集列表
     */
    @Override
    public List<DsCustomDataVo> queryList(DsCustomDataBo bo) {
        LambdaQueryWrapper<DsCustomData> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DsCustomData> buildQueryWrapper(DsCustomDataBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DsCustomData> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCustomName()), DsCustomData::getCustomName, bo.getCustomName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), DsCustomData::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增自定义数据集
     */
    @Override
    public Boolean insertByBo(DsCustomDataBo bo) {
        DsCustomData add = BeanUtil.toBean(bo, DsCustomData.class);
        validEntityBeforeSave(add);
        add.setTypeList(JSONArray.parse(add.getTypeList()).toJSONString());
        add.setDataList(JSONArray.parse(add.getDataList()).toJSONString());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCustomId(add.getCustomId());
        }
        return flag;
    }

    /**
     * 修改自定义数据集
     */
    @Override
    public Boolean updateByBo(DsCustomDataBo bo) {
        DsCustomData update = BeanUtil.toBean(bo, DsCustomData.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DsCustomData entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除自定义数据集
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
